﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('Base_Format.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('Base_Format.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除
      </a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <!--    <div class="table-page-search-wrapper">-->
    <!--      <a-form layout="inline">-->
    <!--        <a-row :gutter="10">-->
    <!--          <a-col :md="4" :sm="24">-->
    <!--            <a-form-item label="查询类别">-->
    <!--              <a-select allowClear v-model="queryParam.condition">-->
    <!--                &lt;!&ndash;<a-select-option key="ParentId">父级ID</a-select-option>&ndash;&gt;-->
    <!--                &lt;!&ndash;<a-select-option key="Code">自定义编码</a-select-option>&ndash;&gt;-->
    <!--                <a-select-option key="Name">名称</a-select-option>-->
    <!--                <a-select-option key="Remark">备注</a-select-option>-->
    <!--              </a-select>-->
    <!--            </a-form-item>-->
    <!--          </a-col>-->
    <!--          <a-col :md="4" :sm="24">-->
    <!--            <a-form-item>-->
    <!--              <a-input v-model="queryParam.keyword" placeholder="关键字" />-->
    <!--            </a-form-item>-->
    <!--          </a-col>-->
    <!--          <a-col :md="6" :sm="24">-->
    <!--            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>-->
    <!--            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>-->
    <!--          </a-col>-->
    <!--        </a-row>-->
    <!--      </a-form>-->
    <!--    </div>-->

    <a-table
      v-if="data && data.length"
      ref="table"
      :scroll="{y:530}"
      :columns="columns"
      :rowKey="row => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      :defaultExpandAllRows="true"
      size="small"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('Base_Format.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('Base_Format.Edit')&&hasPerm('Base_Format.Delete')" type="vertical" />
          <a v-if="hasPerm('Base_Format.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'

const columns = [
  // { title: '父级信息', dataIndex: 'ParentId', width: '10%' },
  // { title: '排序', dataIndex: 'Sort', width: '10%' },
  // { title: '名称', dataIndex: 'Name', width: '10%' },
  // { title: '自定义编码', dataIndex: 'Code', width: '10%' },
  // { title: '备注', dataIndex: 'Remark', width: '10%' },
  // { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }
  { title: '名称', dataIndex: 'Text', width: '10%' },
  { title: '编码', dataIndex: 'Code', width: '10%' },
  { title: '备注', dataIndex: 'Remark', width: '10%' },
  // { title: '排序', dataIndex: 'Sort', width: '10%' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => `总数:${total}`
      },
      filters: {},
      sorter: { field: 'Sort', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/Base_BasicData/Base_Format/GetTreeDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建业态属性档案')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑业态属性档案')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/Base_BasicData/Base_Format/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    }
  }
}
</script>
